package com.example.ying.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.ying.model.domain.Rate;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface RateMapper extends BaseMapper<Rate> {

    // 获取与给定电影相关的所有用户
    @Select("SELECT distinct user_id FROM rate WHERE movie_id = #{movieId}")
    List<String> getUserIdByMovieId(int movieId);

    // 获取指定用户指定电影的评分
    @Select("SELECT rate FROM rate WHERE user_id = #{userId} AND movie_id = #{movieId}")
    Integer getRateByUserIdAndMovieId(String userId, Integer movieId);

    // 获取指定用户的评分详情
    @Select("SELECT * FROM rate WHERE user_id = #{userId}")
    List<Rate> getInfoByUserId(String userId);

    // 获取指定用户评分最高的3部电影
    @Select("SELECT movie_id FROM rate WHERE user_id = #{userId} ORDER BY rate DESC LIMIT 3")
    List<Integer> getTop3MoviesByUserId(String userId);


    // 获取指定用户评分最高的2部电影
    @Select("SELECT movie_id FROM rate WHERE user_id = #{userId} ORDER BY rate DESC LIMIT 2")
    List<Integer> getTop2MoviesByUserId(String userId);


    // 获取指定用户是否已经评分过指定电影
    @Select("SELECT * FROM rate WHERE user_id = #{userId} AND movie_id = #{index}")
    Integer existRateByUserIdAndMovieId(String userId, int index);


    // 获取指定用户已经评分过的电影列表
    @Select("SELECT movie_id FROM rate WHERE user_id = #{userId}")
    List<Integer> getdoubanIdByUserId(String userId);


    // 获取指定用户对指定电影的评分
    @Select("SELECT rate FROM rate WHERE user_id = #{userId} AND movie_id = #{movieId}")
    Integer selectRateByUserIdAndMovieId(String userId, Integer movieId);


    // 删除指定用户对指定电影的评分
    @Delete("DELETE FROM rate WHERE user_id = #{userId} AND movie_id = #{movieId}")
    boolean deleteByMovieIdAndUserId( Integer movieId,String userId );

    //
}
